<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/css/table.css" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/dwz/themes/default/style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="${pageContext.request.contextPath}/dwz/themes/css/core.css" rel="stylesheet" type="text/css" media="screen" />
<script src="${pageContext.request.contextPath}/jquery/jquery-2.1.4.min.js"></script>
<script src="${pageContext.request.contextPath}/laydate/laydate.js" ></script>
<table width="100%" border="0" cellspacing="0" class="table_padding">
	<tr>
		<td align="center">
			<table border="0" cellpadding="0" cellspacing="0" class="table_border">
				<tr>
					<td align="center">
						<table border="0" cellpadding="0" cellspacing="0" class="box_table" id="box_table">
							<tr>
								<td width="22%">&nbsp;用户姓名：<input type="text" value="${searchName}" id="search_name"/></td>
								<td width="38%">
									失效时间：<input type="text" size="20" value="${startTime}" id="start_time"/>至 <input type="text" value="${endTime}" size="20" id="end_time"/>
								</td>
								<td width="25%">锁定状态：

									<c:if test="${lock==0}">
										<input type="checkbox" style="vertical-align: middle;" checked name="lock" value="0"/> 锁定
										<input type="checkbox" style="vertical-align: middle;" name="lock" value="1"/> 启用
									</c:if>

									<c:if test="${lock==1}">
										<input type="checkbox" style="vertical-align: middle;"  name="lock" value="0"/> 锁定
										<input type="checkbox" style="vertical-align: middle;" name="lock" checked value="1"/> 启用
									</c:if>

									<c:if test="${lock==null}">
										<input type="checkbox" style="vertical-align: middle;" name="lock" value="0"/> 锁定
										<input type="checkbox" style="vertical-align: middle;" name="lock" value="1"/> 启用
									</c:if>
								</td>
								<td id="searchBtn">
									<a class="button" href="javascript:void(0)" onclick="searchList()" ><span>查询</span></a>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			<table border="0" cellpadding="0" cellspacing="0" class="table_border">
				<tr>
					<td>
						<table style="position : relative;left : 5px; top : 5px;">
							<tr>
								<td width="75px" align="right" id="detailBtn">
									<a class="button" href="javascript:void(0);" onclick="getDetail()"><span>查看明细</span></a>
								</td>
								<td width="50px" id="addBtn">
									<a class="button" href="user?method=getUserAdd"><span>新增</span></a>
								</td>
								<td width="50px" id="delBtn">
									<a class="button" href="javascript:void(0);" onclick="deleteRole()"><span>删除</span></a>
								</td>
								<td width="50px" id="updateBtn">
									<a class="button" href="javascript:void(0);" onclick="update()"><span>修改</span></a>
								</td>
								<td width="50px" id="startBtn">
									<a class="button" href="javascript:void(0);" onclick="updateLockStatus(1)"><span>启用</span></a>
								</td>
								<td width="50px" id="lockBtn">
									<a class="button" href="javascript:void(0);" onclick="updateLockStatus(0)"><span>锁定</span></a>
								</td>
								<td width="70px" id="assignBtn">
									<a class="button" href="javascript:void(0);" onclick="assign()"><span>分配角色</span></a>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr id="tr-2">
					<td height=110>
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td align="center">
									<table border="0" cellpadding="0" cellspacing="0"
										id="box_num_table2" class="box_num_table">
										<thead>
											<tr>
												<td width="8%"><input type="checkbox" onclick="checkAll(this.checked)"/></td>
												<td width="7%">序号</td>
												<td width="12%">登录帐号</td>
												<td width="11%">用户姓名</td>
												<td width="12%">创建时间</td>
												<td width="13%">失效时间</td>
												<td width="11%">允许访问IP</td>
												<td width="11%">状态</td>
											</tr>
										</thead>
										<tbody id="tInfo2">
										<c:forEach items="${pageInfo.list}" var="user" varStatus="i">
											<tr class="odd">
												<td><input type="checkbox" class="checkbox-user" value="${user.id}"/></td>
												<td>${i.index+1}</td>
												<td>${user.accountNo}</td>
												<td>${user.name}</td>
												<td>${user.createTime}</td>
												<td>${user.expireTime}</td>
												<td>${user.allowIps}</td>
												<c:if test="${user.lockStatus  == '1'}">
													<td class="lock">启用</td>
												</c:if>
												<c:if test="${user.lockStatus  == '0'}">
													<td class="lock">锁住</td>
												</c:if>
											</tr>
										</c:forEach>
										</tbody>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			<table border="0" cellpadding="0" cellspacing="0" class="table_border">
                <tr>
                    <td>
                        <table width="100%" border="0" cellpadding="0" cellspacing="0"
                               class="table_padding">
                            <tr>
                                <td width="8%" class="font_left">数据:${pageInfo.total}条</td>
                                <td width="12" class="font_left">第</td>
                                <td width="375" class="font_left">
                                    <input name="textfield2" value="${pageInfo.pageNum}" id="pageNo" type="text" size="2" maxlength="4" /> /${pageInfo.pages}页
									<input type="button" value="跳转" onclick="jumpPage()">
						每页显示的条数<select>
										<option>-请选择-</option>
										<option value="3">3</option>
										<option value="5">5</option>
										<option value="10">10</option>
									</select>
                                </td>
                                <td width="478" class="font_right">
                                    <a href="javascript:void (0)" onclick="firstPage()">首页</a>
                                    <a href="javascript:void (0)" onclick="beforePage()">上一页</a>
                                    <a href="javascript:void (0)" onclick="nextPage()">下一页</a>
                                    <a href="javascript:void (0)" onclick="lastPage()">末页</a>
                                    <a href="javascript:void (0)">刷新</a>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
		</td>
	</tr>
</table>
<script>
	//执行一个laydate实例
	laydate.render({
		elem: '#start_time', //指定元素
		type:'datetime'
	});

	//执行一个laydate实例
	laydate.render({
		elem: '#end_time', //指定元素
		type:'datetime'
	});
</script>
<script>
	$(function () {
		let permission = '${permissionAll}';
		if (permission.indexOf("user?method=searchUser")<0){
			$("#searchBtn").hide()
		}
		if (permission.indexOf("user?method=getUserDetail")<0){
			$("#detailBtn").hide()
		}
		if (permission.indexOf("user?method=getUserAdd")<0){
			$("#addBtn").hide()
		}
		if (permission.indexOf("user?method=deleteUser")<0){
			$("#delBtn").hide()
		}
		if (permission.indexOf("user?method=getUserUpdate")<0){
			$("#updateBtn").hide()
		}
		if (permission.indexOf("user?method=updateUser")<0){
			$("#startBtn").hide()
		}
		if (permission.indexOf("user?method=updateUser")<0){
			$("#lockBtn").hide()
		}
		if (permission.indexOf("user?method=getUserAssign")<0){
			$("#assignBtn").hide()
		}


	})
</script>
<script>
	/**
	 * 上一页
	 */
	function beforePage() {
		let json = searchHistory();
		var isFirstPage = ${pageInfo.hasPreviousPage};
		if (!isFirstPage) {
			alert("已经是首页了");
			return;
		}
		location.href = "${pageContext.request.contextPath}/user?method=searchUser&pageNo=${pageInfo.prePage}"+json;
	}

	/**
	 * 下一页
	 */
	function nextPage() {
		let json = searchHistory();
		var hasNextPage = ${pageInfo.hasNextPage};
		if (!hasNextPage) {
			alert("已经到底了");
			return;
		}
		location.href = "${pageContext.request.contextPath}/user?method=searchUser&pageNo=${pageInfo.nextPage}"+json;
	}

	/**
	 * 首页
	 */
	function firstPage() {
		let json = searchHistory();
		var isFirstPage = ${pageInfo.isFirstPage};
		if (isFirstPage) {
			alert("这里已经是首页");
			return;
		}
		location.href = "${pageContext.request.contextPath}/user?method=searchUser&pageNo=${pageInfo.navigateFirstPage}"+json;
	}


	/**
	 * 末页
	 */
	function lastPage() {
		let json = searchHistory();
		var isLastPage = ${pageInfo.isLastPage};
		if (isLastPage) {
			alert("已经到底了");
			return;
		}
		location.href = "${pageContext.request.contextPath}/user?method=searchUser&pageNo=${pageInfo.navigateLastPage}"+json;
	}

	/**
	 * 跳转指定页
	 */
	function jumpPage() {
		let json = searchHistory();
		let pageNo = $("#pageNo").val();
		location.href = "${pageContext.request.contextPath}/user?method=searchUser&pageNo=" + pageNo +json;
	}

	/**
	 * 跳转查询用户视图
	 */
	function getDetail() {
		let checked = $(".checkbox-user:checked")[0];
		if (checked == null) {
			alert("没有选中用户,请选择一个用户")
			return
		}
		console.log(checked)
		location.href = "${pageContext.request.contextPath}/user?method=getUserDetail&id=" + checked.value;
	}

	/**
	 * 全选 复选框
	 * @param status
	 */
	function checkAll(status) {
		let checkboxList = $(".checkbox-user");
		for (let i = 0; i < checkboxList.length; i++) {
			checkboxList[i].checked = status;
		}
	}


	/**
	 * 多选框 删除 用户
	 */
	function deleteRole() {
		let checked = $(".checkbox-user:checked");
		let array = [];
		for (let i = 0; i < checked.length; i++) {
			array.push(checked[i].value)
		}
		$.post("user?method=deleteUser", {"array[]": array}, result=> {
			if (result.code == "200") {
				location.href = "user?method=searchUser";
			} else {
				alert(result.message);
			}
		}, "json")
	}

	/**
	 * 跳转更新视图
	 */
	function update() {
		let checked = $(".checkbox-user:checked")[0];
		if (checked == null) {
			alert("没有选中用户,请选择一个用户")
			return;
		}
		location.href = "user?method=getUserUpdate&id=" + checked.value;
	}

	/**
	 * 复合条件查询
	 */
	function searchList() {
		let json = searchHistory();
		location.href = "user?method=searchUser"+json;
	}


	/**
	 *  返回,拼接的复杂条件
	 * @returns {string}
	 */
	function searchHistory() {
		var search_name = $("#search_name").val();
		var start_time = $("#start_time").val();
		var end_time = $("#end_time").val();
		var lock = $(":checkbox[name='lock']:checked");
		var pageNo = $("select option:selected").val()

		var json = "";
		json+="&pageNo="+pageNo;
		if (search_name!=""){
			json+="&search_name="+search_name;
		}
		if (start_time!=""){
			json+="&start_time="+start_time;
		}
		if (end_time!=""){
			json+="&end_time="+end_time;
		}
		if (lock.length==1){
			json+="&lock="+lock.val();
		}

		return json;
	}

	function updateLockStatus(lockStatus) {
		let checked = $(".checkbox-user:checked")[0];
		if (checked == null) {
			alert("没有选中角色,请选择一个角色")
			return;
		}
		let json ={
			"lockStatus":lockStatus,
			"id":checked.value
		}
		let lock = $(".checkbox-user:checked").parent().parent().children().eq(7);
		$.post("user?method=updateUser",json,result=>{
			if (result.code=="200"){
				alert("更新成功");
				if (lockStatus==1){
					lock.text("启用");
				}else {
					lock.text("锁住");
				}
			}else {
				alert("更新失败");
			}
		},"json")

	}

	/**
	 * 分配角色
	 */
	function assign() {
		let checked = $(".checkbox-user:checked")[0];
		if (checked == null) {
			alert("没有选中用户,请选择一个用户")
			return;
		}
		location.href = "user?method=getUserAssign&id="+checked.value;
	}

</script>